from django.http import JsonResponse
from django.shortcuts import render, HttpResponse

# 将发送的消息保存到数据库，我们这里用列表代替一下
DB = []


def home(request):
    """主页面"""
    return render(request, "home.html")


def send_msg(request):
    """发送消息并保存"""
    text = request.GET.get("text")
    DB.append(text)
    return HttpResponse("发送完成！")


def get_msg(request):
    """获取数据库中的消息"""
    index = request.GET.get("index")
    index = int(index)
    centent = {
        "data": DB[index:],
        "index": len(DB)  # 数据的条数
    }
    return JsonResponse(centent)
